package com.dgut.shopManage.entity.dto;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.dgut.common.util.Rent;
import com.dgut.common.util.Stype;
import com.dgut.shopManage.entity.Merchant;
import com.dgut.shopManage.entity.Shop;

public class ShopQueryDTO {
	private Long id;
	private Stype stype;      //店铺类型
	private Rent rent;      //是否被租用
	private String address;
	private Merchant merchant;
	private String shopNo;
	
	public Merchant getMerchant() {
		return merchant;
	}
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}
	public Stype getStype() {
		return stype;
	}
	public void setStype(Stype stype) {
		this.stype = stype;
	}

	public Rent getRent() {
		return rent;
	}
	public String getShopNo() {
		return shopNo;
	}
	public void setShopNo(String shopNo) {
		this.shopNo = shopNo;
	}
	public void setRent(Rent rent) {
		this.rent = rent;
	}

	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public void setMerchant(Merchant merchant) {
		this.merchant = merchant;
	}

	
	public static Specification<Shop> getSpecification(ShopQueryDTO shopQueryDTO)
	{
		Specification<Shop> spec = new Specification<Shop>() {
			public Predicate toPredicate(Root<Shop> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				
				 if(null != shopQueryDTO && !StringUtils.isEmpty(shopQueryDTO.getAddress())) {
					 Predicate  p1 =  cb.equal(root.get("address").as(String.class),shopQueryDTO.getAddress());
					 list.add(p1);
				 }
				 if(null != shopQueryDTO && !StringUtils.isEmpty(shopQueryDTO.getStype())) {
					 Predicate  p2 =  cb.equal(root.get("stype").as(String.class), shopQueryDTO.getStype());
					 list.add(p2);
				 }
				 if(null != shopQueryDTO && !StringUtils.isEmpty(shopQueryDTO.getShopNo())) {
					 Predicate  p3 =  cb.equal(root.get("shopNo").as(String.class),shopQueryDTO.getShopNo());
					 list.add(p3);
				 }

				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}
}
